12. Clustering Objects

Clustering Objects

Now that you have filtered out the table plane, and all points outside of the region of interest, your point cloud should look like the image above. You are now ready to use PCL's Euclidean Clustering algorithm to segment the remaining points into individual objects.

Euclidean Clustering

In order to perform Euclidean Clustering, you must first construct a k-d tree from the cloud_objects point cloud.

The k-d tree data structure is used in the Euclidian Clustering algorithm to decrease the computational burden of searching for neighboring points. While other efficient algorithms/data structures for nearest neighbor search exist, PCL's Euclidian Clustering algorithm only supports k-d trees.

To construct a k-d tree, you first need to convert your XYZRGB point cloud to XYZ, because PCL's Euclidean Clustering algorithm requires a point cloud with only spatial information. To create this colorless cloud, (which I'll call white_cloud), search again in pcl_helper.py to find the function you need to convert XYZRGB to XYZ. Next, construct a k-d tree from it. To accomplish this, add the following code to the pcl_callback() function in your node:

# Euclidean Clustering
white_cloud = # Apply function to convert XYZRGB to XYZ
tree = white_cloud.make_kdtree()

Once your k-d tree has been constructed, you can perform the cluster extraction like this:

# Create a cluster extraction object
ec = white_cloud.make_EuclideanClusterExtraction()
# Set tolerances for distance threshold 
# as well as minimum and maximum cluster size (in points)
# NOTE: These are poor choices of clustering parameters
# Your task is to experiment and find values that work for segmenting objects.
ec.set_ClusterTolerance(0.001)
ec.set_MinClusterSize(10)
ec.set_MaxClusterSize(250)
# Search the k-d tree for clusters
ec.set_SearchMethod(tree)
# Extract indices for each of the discovered clusters
cluster_indices = ec.Extract()

cluster_indices now contains a list of indices for each cluster (a list of lists). In the next step, you'll create a new point cloud to visualize the clusters by assigning a color to each of them.